﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Report.IBLL;
using CNKI.TPI.Web.Search.IBLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CNKI.TPI.Web.UI.Controllers.Admin
{
    public class Admin_ReportController : BaseController
    {
        IDBStatisticsService reportService = null;
        IUserOperateReportService userRptService = null;
        IUserCenterService centerService = null;
        IUserService userService = null;
        public Admin_ReportController(IDBStatisticsService reportService, IUserOperateReportService userRptService, IUserCenterService centerService, IUserService userService)
        {
            this.reportService = generator.CreateInterfaceProxyWithTarget<IDBStatisticsService>(reportService, new IInterceptor[] { new LogInterceptor() });
            this.userRptService = generator.CreateInterfaceProxyWithTarget<IUserOperateReportService>(userRptService, new IInterceptor[] { new LogInterceptor() });
            this.centerService = generator.CreateInterfaceProxyWithTarget<IUserCenterService>(centerService, new IInterceptor[] { new LogInterceptor() });
            this.userService = generator.CreateInterfaceProxyWithTarget<IUserService>(userService, new IInterceptor[] { new LogInterceptor() });

        }
        //
        // GET: /Admin_Report/
        /// <summary>
        /// 资源访问统计 初始化
        /// </summary>
        /// <returns></returns>
        public ActionResult DBStatistical()
        {
            Session["reportdata"] = null; //页面初次加载时，不显示报表文件
            ViewBag.RPTName = "DBStatistic"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/DBStatisticalRDLC.rdlc"; //报表路径
            ViewBag.ClSList = reportService.GetTemplateClS();
            return View("DBStatistical");
        }
        /// <summary>
        /// 资源访问统计 查询
        /// </summary>
        /// <param name="dbname"></param>
        /// <param name="clscode"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <returns></returns>
        public bool DBStatisticalSearch(string dbname, string clscode, DateTime? startdate, DateTime? enddate)
        {
            DataTable dt = reportService.GetDBStatisticsInfo(startdate, enddate, clscode, dbname);//数据源
            ViewBag.RPTName = "DBStatistic"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/DBStatisticalRDLC.rdlc"; //报表路径
            Session["reportdata"] = dt;
            if (dt != null & dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 用户访问统计初始化
        /// </summary>
        /// <returns></returns>
        public ActionResult UserStatistical()
        {
            Session["reportdata"] = null; //页面初次加载时，不显示报表文件
            ViewBag.RPTName = "UserStatisticData"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/UserStatisticalRDLC.rdlc"; //报表路径
            return View("UserStatistical");
        }
        /// <summary>
        /// 用户访问统计查询
        /// </summary>
        /// <param name="searchType"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <returns></returns>
        public bool UserStatisticalSearch(int searchType, DateTime? startdate, DateTime? enddate, string startip, string endip)
        {
            DataTable dt = userRptService.GetUserOperateStatisticsInfo(searchType, startdate, enddate, startip, endip);//数据源
            ViewBag.RPTName = "UserStatisticData"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/UserStatisticalRDLC.rdlc"; //报表路径
            Session["reportdata"] = dt;
            if (dt != null & dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 用户提交量统计
        /// </summary>
        /// <returns></returns>
        public ActionResult UserSubmitInfo()
        {
            Session["reportdata"] = null; //页面初次加载时，不显示报表文件
            ViewBag.RPTName = "UserSubmitInfo"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/UserSubmitInfoRDLC.rdlc"; //报表路径
            return View("UserSubmitInfo");
        }
        /// <summary>
        /// 用户提交量统计查询
        /// </summary>
        /// <param name="username"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <returns></returns>
        public bool UserSubmitInfoSearch(string username, DateTime? startdate, DateTime? enddate)
        {
            DataTable dt = userRptService.GetSubmitInfo(username, startdate, enddate);//数据源
            ViewBag.RPTName = "UserSubmitInfo"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/UserSubmitInfoRDLC.rdlc"; //报表路径
            Session["reportdata"] = dt;
            if (dt != null & dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 用户工作量统计
        /// </summary>
        /// <returns></returns>
        public ActionResult UserWorkInfo()
        {
            Session["reportdata"] = null; //页面初次加载时，不显示报表文件
            ViewBag.RPTName = "UserWorkInfo"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/UserWorkInfoRDLC.rdlc"; //报表路径
            return View("UserWorkInfo");
        }
        /// <summary>
        /// 用户工作量统计查询
        /// </summary>
        /// <param name="username"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <returns></returns>
        public bool UserWorkInfoSearch(string username, DateTime? startdate, DateTime? enddate)
        {
            DataTable dt = userRptService.GetUserWorkInfo(username, startdate, enddate);//数据源
            ViewBag.RPTName = "UserWorkInfo"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/UserWorkInfoRDLC.rdlc"; //报表路径
            Session["reportdata"] = dt;
            if (dt != null & dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 用户操作统计
        /// </summary>
        /// <returns></returns>
        public ActionResult OperatorInfo()
        {
            Session["reportdata"] = null; //页面初次加载时，不显示报表文件
            ViewBag.RPTName = "SearchData"; //数据源名称
            ViewBag.RPTPath = "~/ReportInfo/SearchHistoryRDLC.rdlc"; //报表路径
            ViewBag.UserList = userService.GetAllUser();
            return View("OperatorInfo");
        }
        /// <summary>
        /// 用户工作量统计查询
        /// </summary>
        /// <param name="username"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <returns></returns>
        public string OperatorInfoSearch(int type, string userid, string title, string dbname, DateTime? startdate, DateTime? enddate, string startip, string endip)
        {
            DataTable dt = new DataTable();
            string RPTName = "ViewData"; //数据源名称
            string RPTPath = "~/ReportInfo/ViewHistoryRDLC.rdlc"; //报表路径
            if (type == 1)//浏览历史
            {
                dt = centerService.GetViewPage(userid, dbname, title, startdate, enddate, startip, endip);//数据源
            }
            else if (type == 0)//检索
            {
                dt = centerService.GetSearchPage(userid, dbname, startdate, enddate, startip, endip);//数据源
                RPTName = "SearchData"; //数据源名称
                RPTPath = "~/ReportInfo/SearchHistoryRDLC.rdlc"; //报表路径
            }
            else if (type == 2) //下载
            {
                dt = centerService.GetDownPage(userid, dbname, title, startdate, enddate, startip, endip);//数据源
                RPTName = "DownData"; //数据源名称
                RPTPath = "~/ReportInfo/DownHistoryRDLC.rdlc"; //报表路径
            }
            Session["reportdata"] = dt;
            bool m = false;
            if (dt != null & dt.Rows.Count > 0)
            {
                m = true;
            }
            return "[{\"msg\":\"" + m + "\",\"RPTName\":\"" + RPTName + "\",\"RPTPath\":\"" + RPTPath + "\"}]"; //下拉列表 html串

        }
    }
}
